import configparser
import pymysql

'''
数据库连接取配置文件（config.ini文件）信息
'''
config = configparser.ConfigParser()

# 读取配置文件
config.read('./config.ini')
# config.get('Database1', 'host')

print(config['Database1']['host'])
if __name__ == "__main__":
    connection = pymysql.connect(
        host=config["Database1"]['host'],
        port=3306,
        user=config["Database1"]['user'],
        password=config["Database1"]['password'],
        database='hotmaxx_crm'
    )

    try:
        cursor = connection.cursor()
        sql = "select * from api_auto_test_liulu"
        cursor.execute(sql)

        # 获取查询结果，默认查询结果是元组的格式
        my_result = cursor.fetchall()

        # 将查询结果转换为 JSON 格式
        columns = [desc[0] for desc in cursor.description]
        data = [dict(zip(columns, row)) for row in my_result]
        print(data)
    finally:
        connection.close()
